package com.njtoyo.taxi.admin.service.business.driver.impl;

import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.library.common.OperationRecordHelper;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverChangeInformationReview;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverChangeInformationReviewMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.driver.DriverAuditInformationPresenter;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.AdminUserIdentityPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.driver.DriverAuditInformationQueryWrapper;
import com.njtoyo.taxi.admin.service.business.driver.DriverAuditInformationService;
import com.taxi.entity.common.RestResult;
import com.taxi.entity.common.ResultEnum;
import com.njtoyo.taxi.entity.backend.AdminUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Service
public class DriverAuditInformationServiceImpl implements DriverAuditInformationService {
    @Autowired
    private DriverChangeInformationReviewMapper driverChangeInformationReviewMapper;
    @Autowired
    private OperationRecordHelper operationRecordHelper;

    /**
     * 司机审核列表
     *
     * @param adminUser
     * @param wrapper
     * @return
     */
    @Override
    public RestResult<Page<DriverAuditInformationPresenter>> getList(AdminUser adminUser,
                                                                     DriverAuditInformationQueryWrapper wrapper) {
        AdminUserIdentityPresenter identity = (AdminUserIdentityPresenter) adminUser.getIdentity();
        switch (adminUser.getAccountType()) {
            case company:
            case canton:
                wrapper.setCantonIds(identity.getCantonIds());
                break;
        }
        Page<DriverAuditInformationPresenter> driverAuditInformationPresenters =
                driverChangeInformationReviewMapper.selectList(wrapper);
        return RestResult.success(driverAuditInformationPresenters);
    }

    /**
     * 司机审核
     *
     * @param id
     * @return
     */
    @Override
    public RestResult review(Long id) {
        DriverChangeInformationReview driverChangeInformationReview = driverChangeInformationReviewMapper.selectById(id);
        if (ObjectUtils.isEmpty(driverChangeInformationReview)) {
            return RestResult.build(ResultEnum.NOT_FOUND);
        }
        int verifiedTimestamp = (int) (System.currentTimeMillis() / 1000);
        new LambdaUpdateChainWrapper<>(driverChangeInformationReviewMapper)
                .eq(DriverChangeInformationReview::getId, id)
                .set(DriverChangeInformationReview::getIsVerified, 1)
                .set(DriverChangeInformationReview::getVerifiedTimestamp, verifiedTimestamp)
                .set(DriverChangeInformationReview::getUpdatedAt, new Date())
                .update();
        operationRecordHelper
                .addBusinessLog(String.format("处理了司机信息修改申请 【 %d 】", id));
        return RestResult.success();
    }
}
